<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="740px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改形象' : '添加形象'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">

      <el-row :gutter="15">
        <el-col :lg="12" :md="10">
          <el-form-item label="原视频:" prop="base_video">
            <video width="320" height="240" controls :src="form.base_video"  v-if="form.base_video"> </video>
          </el-form-item>
        </el-col>
        <!-- <el-col :lg="12" :md="10">
          <el-form-item label="压缩视频:" prop="cut_video">
            <div class="ele-image-upload-list">
              <div class="ele-image-upload-item ele-image-upload-button" @click="modalPicTap('dan','cover','封面图片')">
                <div>
                  <div tabindex="0" class="el-upload el-upload--text">
                    <div class="el-upload-dragger">
                      <i class="el-icon-plus ele-image-upload-icon"></i>
                    </div>
                      <div class="ele-image-upload-item" style="margin:0 0 0 0;" v-if="form.cover!=''">
                      <div class="el-image" >
                        <img :src="form.cover" width="100%" height="100%"  class="el-image__inner" style="object-fit: cover;">
                      </div>
                      <div class="ele-image-upload-close" @click="handleRemove('cover')"><i class="el-icon-close"></i></div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-col> -->
      </el-row>

      <el-row :gutter="15">
        <el-col :lg="12" :md="10">
          <el-form-item label="封面图片:" prop="cover">
            <div class="ele-image-upload-list">
              <div class="ele-image-upload-item ele-image-upload-button" @click="modalPicTap('dan','cover','封面图片')">
                <div>
                  <div tabindex="0" class="el-upload el-upload--text">
                    <div class="el-upload-dragger">
                      <i class="el-icon-plus ele-image-upload-icon"></i>
                    </div>
                      <div class="ele-image-upload-item" style="margin:0 0 0 0;" v-if="form.cover!=''">
                      <div class="el-image" >
                        <img :src="form.cover" width="100%" height="100%"  class="el-image__inner" style="object-fit: cover;">
                      </div>
                      <div class="ele-image-upload-close" @click="handleRemove('cover')"><i class="el-icon-close"></i></div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="10">
          <el-form-item label="形象图片:" prop="decode_img">
            <el-image 
              style="width: 100px; height: 100px"
              :src="form.decode_img" 
              :preview-src-list="imgs"
              :zIndex="9999"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="排序号:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入排序号"
          controls-position="right"
          class="ele-fluid ele-text-left"
        />
      </el-form-item>

      <el-form-item label="展示状态:">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.is_display"
        />
        <el-tooltip
          placement="top"
          content="选择不可见则前端页面不显示该数据"
        >
          <i
            class="el-icon-_question"
            style="vertical-align: middle; margin-left: 8px"
          ></i>
        </el-tooltip>
      </el-form-item>

       
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >保存
      </el-button>
    </template>
    <uploadPictures
      :isChoice="isChoice"
      :visible.sync="modalPic"
      @getPic="getPic"
      :gridBtn="gridBtn"
      :gridPic="gridPic"
      :title="modalTitle"
    ></uploadPictures>
  </ele-modal>
</template>

<script>
  import uploadPictures from "@/components/uploadPictures";
  import EleImageUpload from 'ele-admin/es/ele-image-upload';
  import TinymceEditor from '@/components/TinymceEditor';
  import { add,update } from '@/api/defaultAvatar';
  const DEFAULT_FORM = {
    id: 0,
    cover :'',
    base_video:'',
    decode_img:'',
    is_display :1,
    sort :0
  };

  export default {
    name: 'AvatarEdit',
    components: { EleImageUpload,uploadPictures,TinymceEditor },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      pid:0
    },
    data() {
      return {
        // 视频播放器配置
        config: {
          id: 'demoPlayer1',
          lang: 'zh-cn',
          // 宽度 100%
          fluid: true,
          // 视频地址
          url: 'https://s1.pstatp.com/cdn/expire-1-M/byted-player-videos/1.0.0/xgplayer-demo.mp4',
          // 封面
          poster:
            'https://imgcache.qq.com/open_proj/proj_qcloud_v2/gateway/solution/general-video/css/img/scene/1.png',
          // 开启倍速播放
          playbackRate: [0.5, 1, 1.5, 2],
          // 开启画中画
          pip: true
        },
        modalTitle:'选择图片',
        modalPic: false,
        isChoice: "单选",
        gridBtn: {
          xl: 4,
          lg: 8,
          md: 8,
          sm: 8,
          xs: 8,
        },
        gridPic: {
          xl: 6,
          lg: 8,
          md: 12,
          sm: 12,
          xs: 12,
        },
        // 表单数据
        form: { ...DEFAULT_FORM },
        // 表单验证规则
        rules: {
          name: [
            {
              required: true,
              message: '请输入形象名称',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        imgs:[]
      };
    },
    methods: {
      search(where) {
        // debugger
        this.$refs.select.reload({
          where: where,
          page: 1
        });
      },
      // 选择图片
      modalPicTap(tit, picTit,openTitle) {
        this.modalTitle = openTitle;
        this.isChoice = tit === "dan" ? "单选" : "多选";
        this.picTit = picTit;
        this.modalPic = true;
      },
      // 选中图片
      getPic(pc) {
        switch (this.picTit) {
          case "cover":
            this.form.cover = pc.satt_dir;
            break;
          
        }
        this.modalPic = false;
      },
      //删除图片
      handleRemove(field) {
        this.form[field] = '';
      },

      /* 保存编辑 */
      save() {
        this.$refs['form'].validate((valid) => {
          if (!valid) {
            return false;
          }

          this.loading = true;
          const data = {
            ...this.form,
          };

          const saveOrUpdata = this.isUpdate ? update : add;

          saveOrUpdata(data).then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.updateVisible(false);
            this.$emit('done',{ page_id: this.pid });
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {

            this.$util.assignObject(this.form, {
              ...this.data
            });

            this.imgs = [
              this.data.decode_img
            ];

            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs['form'].clearValidate();
          this.form = { ...DEFAULT_FORM };
        }
      }
    }
  };
</script>
